
#include "listdiagram.hh"
#include "node.hh"
#include <QFont>
#include <stdio.h>

dsa::ListTextItem::ListTextItem ()
  : QObject ()
  , QGraphicsItemGroup()
  , _node (0)
{
  _items [Element_Color] = new QGraphicsRectItem(this);
  _items [Element_Name] = new QGraphicsSimpleTextItem(this);
  _items [Element_Size] = new QGraphicsSimpleTextItem(this);

  QGraphicsRectItem* itemColor = static_cast<QGraphicsRectItem*>(_items[Element_Color]);
  itemColor->setRect(0, 0, 10, 10);
  itemColor->setPen(QColor (0, 0, 0));
  itemColor->setBrush(QColor(255, 0, 0));
}


unsigned dsa::ListTextItem::GetWidth(Elements element) const
{
  return _items[element]->boundingRect().width();
}

unsigned dsa::ListTextItem::GetHeight() const
{
  unsigned h = 0;
  for (unsigned i=0; i<Element_COUNT; ++i)
    {
      h = h > _items[i]->boundingRect().height() ? h : _items[i]->boundingRect().height();
    }
  return h;
}


void dsa::ListTextItem::SetNode(dsa::Node *node)
{
  if (_node)
    {
      disconnect (_node, SIGNAL(HoverChanged(bool)), this, SLOT(EnableHover(bool)));
    }
  _node = node;
  if (_node)
    {
      connect (_node, SIGNAL(HoverChanged(bool)), this, SLOT(EnableHover(bool)));

      static_cast<QGraphicsRectItem*>(_items[Element_Color])->setBrush(_node->GetColor());
      static_cast<QGraphicsSimpleTextItem*>(_items[Element_Name])->setText(node->GetName());
      static_cast<QGraphicsSimpleTextItem*>(_items[Element_Size])->setText(dsa::Node::GetSizeString(node->GetSize()));
    }
}

void dsa::ListTextItem::SetSizes(unsigned color, unsigned size)
{
  QGraphicsRectItem* itemColor = static_cast<QGraphicsRectItem*>(_items[Element_Color]);
  QGraphicsSimpleTextItem* itemName = static_cast<QGraphicsSimpleTextItem*>(_items[Element_Name]);
  QGraphicsSimpleTextItem* itemSize = static_cast<QGraphicsSimpleTextItem*>(_items[Element_Size]);

  itemColor->setPos(color - itemColor->boundingRect().width(), 0);
  itemSize->setPos(color + 5 + size - itemSize->boundingRect().width(), 0);
  itemName->setPos(color + 5 + size + 5, 0);
}

void dsa::ListTextItem::EnableHover(bool enable)
{
  QFont fnt = static_cast<QGraphicsSimpleTextItem*>(_items[Element_Name])->font();
  fnt.setBold(enable);

  static_cast<QGraphicsSimpleTextItem*>(_items[Element_Name])->setFont(fnt);
  static_cast<QGraphicsSimpleTextItem*>(_items[Element_Size])->setFont(fnt);
}



dsa::ListDiagram::ListDiagram(QWidget *parent)
  : QGraphicsView (parent)
  , _scene (0)
{
  _scene = new QGraphicsScene (this);
  setScene(_scene);
}

void dsa::ListDiagram::Set(dsa::Node *node, QList<dsa::Node *> nodes)
{

  foreach (dsa::ListTextItem* item, _items)
    {
      _scene->removeItem(item);
      delete item;
    }

  _items.clear();

  unsigned maxColorSize = 0;
  unsigned maxSizeSize = 0;
  foreach (dsa::Node* node, nodes)
    {
      if (node->GetType() == dsa::Node::Type_Dummy)
        {
          continue;
        }

      dsa::ListTextItem* textItem = new dsa::ListTextItem ();
      textItem->SetNode(node);
      maxColorSize = maxColorSize > textItem->GetWidth(dsa::ListTextItem::Element_Color) ? maxColorSize : textItem->GetWidth(dsa::ListTextItem::Element_Color);
      maxSizeSize = maxSizeSize > textItem->GetWidth(dsa::ListTextItem::Element_Size) ? maxSizeSize : textItem->GetWidth(dsa::ListTextItem::Element_Size);
      _scene->addItem(textItem);
      _items.push_back(textItem);
    }


  unsigned x = 0;
  unsigned y = 0;
  foreach (dsa::ListTextItem* item, _items)
    {
      item->SetSizes(maxColorSize, maxSizeSize);
      item->setPos(x, y);

      y += item->GetHeight() + 5;
    }
  fflush(stdout);

}
